﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.客户CRM.Service;
using DXApplicationTangche.UC.抖音出入库.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.客户CRM.高德
{
    public partial class Form保存客户地址校验 : DevExpress.XtraEditors.XtraForm
    {
        private GeoDTO geoDTO = new GeoDTO();
        private GeocodeDTO geocodeDTO = new GeocodeDTO();
        public Boolean if全关闭 = true; 
        public Form保存客户地址校验(String name,String mobile,String address,String addressNumber)
        {
            InitializeComponent();
            this.textBox姓名.Text = name;
            this.textBox电话.Text = mobile;
            this.textBox地址.Text = address;
            this.textBox门牌号.Text = addressNumber;
            this.geoDTO = 高德地址Service.checkSaveAddress(address);
            if (this.geoDTO.geocodes.Count > 0)
            {
                this.gridControl高德地址.DataSource = this.geoDTO.geocodes;
                this.gridView高德地址.RefreshData();
                this.显示首条地址();
            }
            else
            {
                MessageBox.Show("高德地图没有该地址");
            }
        }

        private void 显示首条地址()
        {
            try
            {
                this.geocodeDTO = this.geoDTO.geocodes[0];
                //显示首条地址
                this.pictureBox1.LoadAsync("https://restapi.amap.com/v3/staticmap?location=" + this.geoDTO.geocodes[0].location + "&zoom=10&size=750*300&markers=mid,,A:" + this.geoDTO.geocodes[0].location + "&key=1e35c2196190d3f06060fb90f1c4ce30");
            }
            catch { }
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            DouyinCustomerAddressDTO douyinCustomerAddressDTO =
                new DouyinCustomerAddressDTO(
                this.textBox姓名.Text.Trim(),
                this.textBox电话.Text.Trim(),
                this.geocodeDTO.province,
                this.geocodeDTO.city,
                this.geocodeDTO.district,
                this.geocodeDTO.formatted_address,
                this.textBox门牌号.Text.Trim(),
                this.geocodeDTO.location,
                this.geocodeDTO.level
                );
            //  保存抖音客户地址
            抖音Service.saveDouyinUserInfo(
                    "SSHIRT", 
                    "18",
                    douyinCustomerAddressDTO
                );
            this.if全关闭 = true;
            this.Close();
        }

        private void gridView高德地址_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                List<String> level = new List<String>();
                level.Add("热点商圈");
                level.Add("兴趣点");
                level.Add("门牌号");
                if (e.Column.Name == "gridColumn高德地理编码匹配级别")
                {
                    if (!level.Contains(this.geoDTO.geocodes[e.RowHandle].level))
                    {
                        e.Appearance.BackColor = Color.Red;
                    }
                }
            }
            catch { }
        }

        private void simpleButton关闭_Click(object sender, EventArgs e)
        {
            this.if全关闭 = false;
            this.Close();
        }
    }
}